//
//  TagTableViewController.m
//  AIM
//
//  Created by user10 on 2017/5/18.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "TagTableViewController.h"
#import "tagCell.h"
#import "typeTableViewCell.h"
#import "tag.h"
#import "NetWorkTools.h"

@interface TagTableViewController ()

@property (nonatomic, strong) NSMutableArray *selectArray;
@property (nonatomic, strong) NSArray *tagArr;
@property (nonatomic, strong) NSArray *resultArray;

@end

@implementation TagTableViewController


- (NSArray *)resultArray
{
    if (!_resultArray) {
        _resultArray = [NSMutableArray array];
    }
    return _resultArray;
}

- (NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

/** 加载数据 */
//- (NSArray *)tagArr {
//    if (nil == _tagArr) {
//        NSArray *tagArray = self.resultArray;
//        NSMutableArray *mgroupsArray = [NSMutableArray array];
//        for (NSDictionary *tagDict in tagArray) {
//            tag *tagResult = [tag tagWithDictionary:tagDict];
//            [mgroupsArray addObject:tagResult];
//        }
//        self.tagArr = mgroupsArray;
//    }
//    return _tagArr;
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _selectArray = [NSMutableArray array];
    _resultArray = [NSArray array];
    NSLog(@"=======%@",_historyArr);
    
    
    self.tableView.allowsMultipleSelectionDuringEditing=YES;
    NSString *webServiceStr = [NSString stringWithFormat:
                               @"<web:getPostTagData>\
                               <arg0>%@</arg0>\
                               </web:getPostTagData>",COMPANY];
    //初始化网络工具类
    NetWorkTools *tools = [NetWorkTools shareTools];
    //发送网络请求
    [tools request:POST urlString:[NSString stringWithFormat:@"http://%@:8888/webservice/ws/PostTagImplPort?wsdl",[NSUD objectForKey:@"URL"]] parametersStr:webServiceStr finished:^(id responseObject, NSError *error) {
        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *string = result;
        NSRange startRange = [string rangeOfString:@"@@"];
        NSRange endRange = [string rangeOfString:@"</return>"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        if (range.location != NSNotFound) {
            NSString *resultString = [string substringWithRange:range];
            
            NSData * getJsonData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];
            _resultArray= [getDict objectForKey:@"DATA"];
            NSMutableArray *mgroupsArray = [NSMutableArray array];
            for (NSDictionary *tagDict in _resultArray) {
                tag *tagResult = [tag tagWithDictionary:tagDict];
                [mgroupsArray addObject:tagResult];
            }
            self.tagArr = mgroupsArray;
            dispatch_async(dispatch_get_main_queue(), ^{
                for (NSDictionary *dic in _historyArr) {
                    NSString *MSG_TAG = [dic objectForKey:@"MSG_TAG"];
                    for (int i = 0; i < self.tagArr.count; i++) {
                        tag *tagResult = self.tagArr[i];
                        if ([tagResult.MSG_TAG_CD isEqualToString:MSG_TAG]) {
                            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                            [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
                        }
                    }
                }
            });
            
            [self.tableView reloadData];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Network error" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            alert.delegate = self;
            [alert show];
        }
        
        
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tagArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"typeTableViewCell";
    
    typeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (nil == cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"typeTableViewCell"  owner:self options:nil] lastObject];
    }
    tag *tagResult = self.tagArr[indexPath.row];
    cell.textLabel.text = tagResult.MSG_TAG_NM;
    
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark 选中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
    typeTableViewCell *nowCell = [tableView cellForRowAtIndexPath:indexPath];
    tag *tagResult = self.tagArr[indexPath.row];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:tagResult.MSG_TAG_CD forKey:@"SHARE_TAG_CD"];
    [dict setObject:tagResult.MSG_TAG_NM forKey:@"SHARE_TAG_NM"];
    
    if (nowCell.image.hidden) {
        nowCell.selectedBackgroundView = [[UIView alloc] initWithFrame:nowCell.frame];
        nowCell.backgroundColor = [UIColor magentaColor];
        
        nowCell.textLabel.backgroundColor = [UIColor clearColor];
        
        nowCell.image.hidden = NO;
        if ([self.tagDelegate respondsToSelector:@selector(tagValuesWithDict:)]) {
            // 调用代理对象的刷新方法，代理对象去实现方法
            [self.tagDelegate tagValuesWithDict:dict];
        }
//        [_selectArray addObject:dict];
    }else{
        nowCell.image.hidden = YES;
        [_selectArray removeObject:dict];
        nowCell.selectedBackgroundView = [[UIView alloc] initWithFrame:nowCell.frame];
        nowCell.backgroundColor = [UIColor clearColor];
        
        nowCell.textLabel.backgroundColor = [UIColor clearColor];
        if ([self.tagDelegate respondsToSelector:@selector(tagValuesDelFromDict:)]) {
            [self.tagDelegate tagValuesDelFromDict:dict];
        }
    }
}

@end
